package com.lon.over.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lon.over.R;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * author: ctl
 * 创建时间: 2022/4/22
 * Describe:
 */
public abstract class BaseFragment extends Fragment {

    private State currentState = State.NONE;
    private View mSuccessView;
    private View mLoadingView;
    private View mErrorView;
    private View mEmptyView;

    public enum State {
        NONE, LOADING, ERROR, EMPTY, SUCCESS
    }

    private Unbinder mBind;
    private FrameLayout mBaseContainer;

    /**
     * 点击事件 根据butterKnife库进行操作，减少代码量 ，是一个不错的类库
     */
    @OnClick(R.id.network_error_tips)
    public void retry() {
        onRetryClick();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = loadRootView(inflater, container);
        mBaseContainer = rootView.findViewById(R.id.base_fragment);
        loadStatesView(inflater, container);
        mBind = ButterKnife.bind(this, rootView);
        initView(rootView);
        initListener();
        initPresenter();
        loadData();
        return rootView;
    }

    /**
     * 如果子类需要实现点击事件，就覆写该方法
     */
    protected void initListener() {

    }

    // 复写Fragment记载，用于显示顶部导航栏
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.base_fragment_layout, container, false);
    }

    /**
     * 加载各种状态界面
     *
     * @param inflater
     * @param container
     * @return
     */
    protected void loadStatesView(LayoutInflater inflater, ViewGroup container) {
        // 加载成功的View
        mSuccessView = loadSuccessView(inflater, container);
        mBaseContainer.addView(mSuccessView);
        // 加载中的View
        mLoadingView = loadLoadingView(inflater, container);
        mBaseContainer.addView(mLoadingView);
        // 网络错误的VIew
        mErrorView = loadErrorView(inflater, container);
        mBaseContainer.addView(mErrorView);
        // 数据为空的View
        mEmptyView = loadEmptyView(inflater, container);
        mBaseContainer.addView(mEmptyView);
        // 默认设置为空，否则界面冲突
        setUpState(State.NONE);
    }

    /**
     * 子类通过这个方法来切换状态
     *
     * @param state
     */
    public void setUpState(State state) {
        this.currentState = state;
        mSuccessView.setVisibility(currentState == State.SUCCESS ? View.VISIBLE : View.GONE);
        mLoadingView.setVisibility(currentState == State.LOADING ? View.VISIBLE : View.GONE);
        mErrorView.setVisibility(currentState == State.ERROR ? View.VISIBLE : View.GONE);
        mEmptyView.setVisibility(currentState == State.EMPTY ? View.VISIBLE : View.GONE);

    }

    // 数据为空界面
    private View loadEmptyView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_empty, container, false);
    }

    // 错误界面
    private View loadErrorView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_error, container, false);
    }

    // 加载界面
    private View loadLoadingView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_loading, container, false);
    }

    // 加载成功界面
    private View loadSuccessView(LayoutInflater inflater, ViewGroup container) {
        int resId = getRootViewId();
        return inflater.inflate(resId, container, false);
    }

    // 释放内存
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mBind != null) {
            mBind.unbind();
        }
        release();
    }

    // 获取当前界面的id
    protected abstract int getRootViewId();

    // 初始化组件
    protected void initView(View rootView) {

    }

    // 释放资源
    protected void release() {

    }

    // 创建Presenter
    protected void initPresenter() {

    }

    // 加载数据
    protected void loadData() {

    }

    // 当子类Fragment需要重写时，复写该方法即可
    protected void onRetryClick() {

    }
}
